package com.ssi.report.common.jdbc;

/** 
 * 
 * 	Developed By 	: Kroekpong Sakulchai
 *  Date 			: 09-MAY-2014
 *  Version 		: Alpha
 *  Team			: Software Solution And Integration
 *   
 * **/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.ssi.report.common.constant.ReportConstant;
import com.ssi.report.common.utils.SSLogUtil;

public class JDBCConnection {
	
	public static SSLogUtil logger = new SSLogUtil();
	
	String connectionURL =  ReportConstant.CONFIG_JDBC_URL;
	String driver =  ReportConstant.CONFIG_JDBC_DRIVER;
	String username =  ReportConstant.CONFIG_JDBC_USERNAME;
	String password =  ReportConstant.CONFIG_JDBC_PASSWORD;
	
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;


	public ResultSet queryBySql(String sql) {
		
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(connectionURL, username,	password);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			
		} catch (ClassNotFoundException e) {
			logger.debug("Driver Error");
		} catch (SQLException e) {
			logger.debug("SQLException: " + e.getMessage());
		}

		return rs;

	}
	
	public Statement getStatement() {
		
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(connectionURL, username,	password);
			stmt = conn.createStatement();
			
		} catch (ClassNotFoundException e) {
			logger.debug("Driver Error");
		} catch (SQLException e) {
			logger.debug("SQLException: " + e.getMessage());
		}
		
		return stmt;
		
	}
	
	public Connection getConnection() {
		
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(connectionURL, username,	password);
			
		} catch (ClassNotFoundException e) {
			logger.debug("Driver Error");
		} catch (SQLException e) {
			logger.debug("SQLException: " + e.getMessage());
		}
		
		return conn;
		
	}
	
}
